package com.buu.education.controller;

import com.alibaba.fastjson.JSON;
import com.buu.common.annotation.Log;
import com.buu.common.utils.PageUtils;
import com.buu.common.utils.Query;
import com.buu.common.utils.R;
import com.buu.education.domain.EducationEvaluateDO;
import com.buu.education.service.EvaluateService;
import com.buu.manager.domain.EduDO;
import com.buu.manager.service.EduService;
import com.buu.system.domain.SysUserDO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 教学评价结果查看
 */
@RequestMapping("/edu_eva_result")
@Controller
public class EducationEvaluateResultController {

    @Autowired
    private EduService eduService;

    @Autowired
    private EvaluateService evaluateService;

    /**
     * 返回评价学生页面
     * @param model
     * @return
     */
    @GetMapping("/student")
    String student(Model model) {
        Subject subject = SecurityUtils.getSubject();
        SysUserDO sysUserDO = (SysUserDO) subject.getPrincipal();
        return "education/evaluate/result/student";
    }

    /**
     * 返回评价教师页面
     * @param model
     * @return
     */
    @GetMapping("/teacher")
    String teacher(Model model) {
        Subject subject = SecurityUtils.getSubject();
        SysUserDO sysUserDO = (SysUserDO) subject.getPrincipal();
        return "education/evaluate/result/teacher";
    }

    /**
     * 获取学生评价结果
     * @param params
     * @return
     */
    @GetMapping("/list_stu")
    @ResponseBody
    PageUtils listStu(@RequestParam Map<String, Object> params) {
        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        SysUserDO sysUserDO = (SysUserDO) subject.getPrincipal();
        //根据用户获取身份
        EduDO eduDO = eduService.getUserBySysUserId(sysUserDO.getUserId());
        if(eduDO != null){
            List<EducationEvaluateDO> edus = null;
            int total = 0;
            // 查询列表数据
            Query query = new Query(params);
            //如果是学生，查询本人评价结果
            if("2".equals(eduDO.getType())){
                query.put("userId", sysUserDO.getUserId());
                edus = evaluateService.getEduEvaBySudentId(query);
                total = evaluateService.getEduEvaBySudentIdCount(query);
            } else if("1".equals(eduDO.getType())){
                //如果是老师，查看本人所在班级的所有学生的评价
                query.put("unit", eduDO.getUnit());
                edus = evaluateService.getEduEvaByUnit(query);
                total = evaluateService.getEduEvaByUnitCount(query);
            }else if("0".equals(eduDO.getType())){
                edus = evaluateService.getEduEvaByUnit(query);
                total = evaluateService.getEduEvaByUnitCount(query);
            }

            PageUtils pageUtil = new PageUtils(edus, total);
            return pageUtil;
        }
        return null;
    }

    /**
     * 获取教师评价结果
     * @param params
     * @return
     */
    @GetMapping("/list_tea")
    @ResponseBody
    PageUtils listTea(@RequestParam Map<String, Object> params) {
        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        SysUserDO sysUserDO = (SysUserDO) subject.getPrincipal();
        //根据用户获取身份
        EduDO eduDO = eduService.getUserBySysUserId(sysUserDO.getUserId());
        if(eduDO != null){
            // 查询列表数据
            Query query = new Query(params);
            List<EducationEvaluateDO> edus = null;
            int total = 0;
            //如果是教师用户登录，则只能查看自己的评价结果
            if("1".equals(eduDO.getType())){
                query.put("eduUserId", sysUserDO.getUserId());
                edus = evaluateService.getEduEvaByUnit(query);
                evaluateService.getEduEvaByUnitCount(query);
            }else if("2".equals(eduDO.getType())){
                //如果是学生用户，则只查询自己评价的教师
                query.put("evaUserId", eduDO.getId());
                edus = evaluateService.getEduEvaByEvaSudentId(query);
                evaluateService.getEduEvaByEvaSudentId(query);
            }else if("0".equals(eduDO.getType())){
                //如果是督导用户，查看所有教师评价
                edus = evaluateService.getEduEvaByUnit(query);
                evaluateService.getEduEvaByUnitCount(query);
            }

            PageUtils pageUtil = new PageUtils(edus, total);
            return pageUtil;
        }
        return null;
    }


    @Log("评价查看")
    @GetMapping("/view/{id}")
    String view(Model model, @PathVariable("id") Long id) {
//        EduDO eduDO = eduService.get(id);
//        model.addAttribute("user", eduDO);
        //获取评价对象信息
        EducationEvaluateDO educationEvaluateDO = evaluateService.get(id);
        switch (educationEvaluateDO.getUnit()){
            case "1":
                educationEvaluateDO.setUnit("一年级");
                break;
            case "2":
                educationEvaluateDO.setUnit("二年级");
                break;
            case "3":
                educationEvaluateDO.setUnit("三年级");
                break;
            case "4":
                educationEvaluateDO.setUnit("四年级");
                break;
            case "5":
                educationEvaluateDO.setUnit("五年级");
                break;
            case "6":
                educationEvaluateDO.setUnit("六年级");
                break;
        }
        model.addAttribute("user", educationEvaluateDO);
        return "education/evaluate/view";
    }
}
